import { showWarningMessage } from "../../mapbox_tool/message/index";
import { LayerConfig, addLayer, LayerType, removeLayer } from "../layer/layerManage.ts";
import { getLayerManagerState } from "../layer/layerManagerState.ts";
const defaultRampColors = {
  1.5: "rgba(134,163,171,256)",
  2.5: "rgba(126,152,188,256)",
  4.12: "rgba(110,143,208,256)",
  4.63: "rgba(110,143,208,256)",
  6.17: "rgba(15,147,167,256)",
  7.72: "rgba(15,147,167,256)",
  9.26: "rgba(57,163,57,256)",
  10.29: "rgba(57,163,57,256)",
  11.83: "rgba(194,134,62,256)",
  13.37: "rgba(194,134,63,256)",
  14.92: "rgba(200,66,13,256)",
  16.46: "rgba(200,66,13,256)",
  18.0: "rgba(210,0,50,256)",
  20.06: "rgba(215,0,50,256)",
  21.6: "rgba(175,80,136,256)",
  23.66: "rgba(175,80,136,256)",
  25.21: "rgba(117,74,147,256)",
  27.78: "rgba(117,74,147,256)",
  29.32: "rgba(68,105,141,256)",
  31.89: "rgba(68,105,141,256)",
  33.44: "rgba(194,251,119,256)",
  42.18: "rgba(194,251,119,256)",
  43.72: "rgba(241,255,109,256)",
  48.87: "rgba(241,255,109,256)",
  50.41: "rgba(256,256,256,256)",
  57.61: "rgba(256,256,256,256)",
  59.16: "rgba(0,256,256,256)",
  68.93: "rgba(0,256,256,256)",
  69.44: "rgba(256,37,256,256)"
};
// 定义默认的颜色渐变配置，键为渐变位置，值为对应的颜色
// const defaultRampColors = {
//     0.0: "#3288bd",
//     0.1: "#66c2a5",
//     0.2: "#abdda4",
//     0.3: "#e6f598",
//     0.4: "#fee08b",
//     0.5: "#fdae61",
//     0.6: "#f46d43",
//     1.0: "#d53e4f",
// };

// const defaultWindData = {
//   source: "",
//   date: "",
//   width: 360,
//   height: 180,
//   uMin: -21.32,
//   uMax: 26.8,
//   vMin: -21.57,
//   vMax: 21.42,
//   imageUrl: "/windyData/2016112000.png"
// };

// 定义 WindGL 类，用于绘制风场
let wind: {
  setColorRamp: any;
  numParticles: number;
  windData: any;
  draw: (arg0: any) => void;
  fadeOpacity: number;
  speedFactor: number;
  dropRate: number;
  dropRateBump: number;
  resize: () => void;
  getSpeed: (coordinate: { x: number; y: number }) => any;
} | null = null; // 定义一个全局变量 wind，用于保存 WindGL 实例
// let pxRatio = Math.max(Math.floor(window.devicePixelRatio) || 1, 2); // 获取设备像素比，并设置默认值为 1 或 2

// 用于存储事件监听器的引用，方便后续移除
let wheelListener: () => void;
let dragStartListener: () => void;
let moveListener: () => void;
let mouseMoveListener: (e: any) => void;

// 加载风场数据
export function loadWindy(map: any, id: string, data: any) {
  if (!map) return;

  // 定义需要检查的属性
  const requiredProperties = ["width", "height", "uMin", "uMax", "vMin", "vMax", "imageUrl"];

  // 检查 data 是否存在
  if (!data) {
    showWarningMessage("数据不可为空！");
    return;
  }

  // 检查 data 中的必要属性是否为空
  for (const prop of requiredProperties) {
    if (data[prop] === undefined || data[prop] === null) {
      showWarningMessage("数据属性不可为空！");
      return;
    }
  }

  // 创建windy图层
  const windyLayer: LayerConfig = {
    id,
    type: LayerType.custom,
    renderingMode: "2d",
    onAdd: function (map: any, gl: any) {
      wind = new WindGL(gl);
      wind.numParticles = 65536;
      setWindData(data);

      // 添加事件监听器
      wheelListener = () => {
        if (wind) wind.resize();
      };
      dragStartListener = () => {
        if (wind) wind.resize();
      };
      moveListener = () => {
        if (wind) wind.resize();
      };
      mouseMoveListener = (e: any) => {
        let coordinate = {
          x: e.lngLat.lng,
          y: e.lngLat.lat
        };
        if (wind) {
          wind.getSpeed(coordinate);
        }
      };

      map.on("wheel", wheelListener);
      map.on("dragstart", dragStartListener);
      map.on("move", moveListener);
      map.on("mousemove", mouseMoveListener);
    },
    render: function (gl: any, matrix: any) {
      if (wind && wind.windData) {
        gl.enable(gl.BLEND);
        gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
        wind.draw(matrix);
        map.triggerRepaint();
        return true;
      }
    }
  };
  // map.addLayer(windyLayer);
  addLayer(getLayerManagerState(map), windyLayer);
}
// 设置风场参数    fadeOpacity 粒子轨迹的淡出速度, speedFactor 粒子的移动速度,dropRate 随机重置位置的概率 ,dropRateBump 随机重置位置的概率增加量
export function setWindyOptions(options: any) {
  if (wind) {
    wind.fadeOpacity = options.fadeOpacity || 0.996;
    wind.speedFactor = options.speedFactor || 0.25;
    wind.dropRate = options.dropRate || 0.003;
    wind.dropRateBump = options.dropRateBump || 0.01;
  }
}
// 移除风场图层
export function removeWindy(map: any, id: string) {
  if (map && id) {
    // map.removeLayer(id);
    // map.removeSource(id);
    removeLayer(getLayerManagerState(map), id);
    wind = null;

    // 移除事件监听器
    if (wheelListener) map.off("wheel", wheelListener);
    if (dragStartListener) map.off("dragstart", dragStartListener);
    if (moveListener) map.off("move", moveListener);
    if (mouseMoveListener) map.off("mousemove", mouseMoveListener);
  }
}
// 设置颜色渐变
export function setRampColors(colors: any) {
  if (wind) {
    wind.setColorRamp(colors || defaultRampColors);
  }
}
// 设置风场数据
function setWindData(data: any) {
  if (data.imageUrl) {
    const image = new Image();
    data.image = image;
    image.src = data.imageUrl;
    image.onload = () => {
      if (wind) {
        wind.setWind(data);
      }
    };
  } else {
    showWarningMessage("请提供有效的风场数据");
  }
}
